package com.zlzxm.shoesmarket.view.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.fragment.FMGoodsRequestNote;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMGoodsMatchList;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/22/21
 * <br>
 *  求货单匹配列表
 */
public class ACGoodsMatchList extends BaseActivity{

    public static void start(Activity activity, ImageView sharedElement,
                             String urlFace){

        Intent intent = new Intent(activity,ACGoodsMatchList.class);

        intent.putExtra(StartBundleKey.GOODS_PIC,urlFace);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if(sharedElement != null) {

                activity.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, sharedElement,
                        activity.getString(R.string.transition_goods_pic)).toBundle());
            }else {

                activity.startActivity(intent);

            }

        }else {

            activity.startActivity(intent);

        }

    }


    private VMGoodsMatchList vmGoodsMatchList;

    private ImageView ivFace;

    private TabLayout tabLayout;

    private ViewPager viewPager;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_goods_match);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("求货单匹配列表");
        simpleHead.setOnItemclicklistener(this::finish);

        ivFace = ZViewHelp.findById(this,R.id.ivFace);

        tabLayout = ZViewHelp.findById(this,R.id.tabLayout);

        viewPager = ZViewHelp.findById(this,R.id.viewPager);

        tabLayout.setupWithViewPager(viewPager);

        viewPager.setCurrentItem(1);

        viewPager.setAdapter(new MyPager(getSupportFragmentManager()));

    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmGoodsMatchList = new ViewModelProvider(this).get(VMGoodsMatchList.class);
    }

    @Override
    protected void initData() {
        super.initData();
        String url = getIntent().getStringExtra(StartBundleKey.GOODS_PIC);

        if(!TextUtils.isEmpty(url)) {
            Glide.with(this)
                    .load(url)
                    .centerCrop()
                    .into(ivFace);
        }
    }

    class MyPager extends FragmentPagerAdapter{

        private List<Fragment> list = new ArrayList<>();

        public MyPager(@NonNull FragmentManager fm) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            list.add(FMGoodsRequestNote.newInstance());
            list.add(FMGoodsRequestNote.newInstance());
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0? "出货":"求货";
        }
    }
}
